package com.css.zfzx.sjcj.modules.imageupload.controlller;

import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.sjcj.modules.imageupload.service.ImageUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/hddc/pic")
public class ImageUploadController {
    @Autowired
    ImageUploadService imageUploadService;
    @PostMapping("upload")
    public RestResponse uploadPic(MultipartFile uploadFile) {
        RestResponse restResponse=null;
        try {
            String imageURL = imageUploadService.uploadPic(uploadFile);
            restResponse= RestResponse.succeed(imageURL);
        }catch (Exception e){
            String errormessage="上传失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    @PostMapping("/uploadimags")
    public RestResponse uploadImg(MultipartFile[] multipartFiles, String buildingCode){
        RestResponse response = null;
        try{
            List<String> imgURL = new ArrayList<>();
            if(multipartFiles!=null && multipartFiles.length > 0 && multipartFiles.length <= 5){
                imgURL = imageUploadService.uploadPics(multipartFiles, buildingCode);
                response = RestResponse.succeed(imgURL);
            }else if(multipartFiles.length > 5){
                response = RestResponse.fail("每次上传图片最多不能超过5张!");
            }
        }catch (Exception e){
            String errormessage="上传图片失败";
            log.error(errormessage,e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     *
     * @param uploadFile
     * @param buildingCode
     * @return
     */
    @PostMapping("uploadList")
    public RestResponse uploadPicList(MultipartFile uploadFile, String buildingCode) {
        RestResponse restResponse=null;
        try {
            String imageURL = imageUploadService.uploadPicList(uploadFile,buildingCode);
            restResponse= RestResponse.succeed(imageURL);
        }catch (Exception e){
            String errormessage="上传失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 将base64编码转成图片格式并保存到本地，并返回虚拟路径
     * @param base64Info
     * @param
     * @return
     */
    @PostMapping("/image64")
    public RestResponse image(@RequestBody String[] base64Info, String buildingCode) {

        RestResponse restResponse = null;
        try {
            List<String> imageURL = imageUploadService.uploadBase64(base64Info, buildingCode);
            restResponse = RestResponse.succeed(imageURL);
        } catch (Exception e) {
            String errormessage = "上传失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;

    }

}
